Skip to content

fix: restore operator overloads, fix casts, imports, and compound assignment#55

Merged
danfma merged 5 commits intomainfrom
fix/issues-pack
Apr 14, 2026
Merged

fix: restore operator overloads, fix casts, imports, and compound assignment#55
danfma merged 5 commits intomainfrom
fix/issues-pack

Conversation

@danfma
Copy link
Copy Markdown
Owner

@danfma danfma commented Apr 14, 2026

Summary

Pack of fixes discovered while adding the SampleOperatorOverloading project:

  • Operator auto-naming: derive names from C# token (+add, *multiply) when [Name] is absent — operators were silently skipped before
  • Overload dispatcher for operators: same-token overloads (e.g., Money * bigint vs Money * Money) now generate runtime dispatchers with type checks
  • Numeric cast transpilation: explicit casts between bigint/Decimal/number now emit correct code (new Decimal(x.toString()), BigInt(x.toFixed(0)), etc.)
  • Math.Round/Floor/Ceiling with decimal: rewritten to Decimal.js instance methods when argument is System.Decimal
  • Import merge: multiple imports from the same path consolidated into one line
  • Top-level statement fixes: constlet for mutated locals, relative imports instead of barrel alias, compound assignment (+=) lowered to $add() call
  • BigInt literals: numeric literals implicitly converted to BigInteger emit with n suffix
  • Add SampleOperatorOverloading project with Money record struct and 13 TS tests

Test plan

  • 357 .NET tests pass
  • 455 JS tests pass (18 + 65 + 19 + 13 + existing)
  • Zero TS compilation errors in SampleOperatorOverloading
  • All other samples build and test cleanly

Closes #53
Closes #54

danfma added 3 commits April 13, 2026 22:36
- Auto-derive operator names from C# token when [Name] is absent
  (+ → add, - → subtract, * → multiply, / → divide, etc.)
- Generate overload dispatcher for operators sharing the same token
  with different parameter types (e.g., Money * bigint vs Money * Money)
- Merge same-path imports into a single line in ImportCollector
- Transpile explicit casts between bigint/Decimal/number types
  (BigInteger ↔ decimal ↔ int conversions)
- Rewrite Math.Round/Floor/Ceiling to Decimal.js instance methods
  when the argument is System.Decimal
- Fix const/let detection for top-level statements (use CompilationUnit
  as scope instead of individual GlobalStatementSyntax)
- Fix hasRootIndex for executables (derive from files, not exports)
- Add SampleOperatorOverloading with Money record struct and 13 TS tests
…53)

The compiler-synthesized entry point type has an empty namespace, causing
the import collector to treat same-namespace types as different-namespace
and emit barrel aliases (from "#") instead of relative paths (from "./x").
Now falls back to the project's root namespace when currentNs is empty.

Closes #53
- Compound assignment with user-defined operators (x += y) is now
  lowered to x = x.$add(y) when the semantic model resolves the
  operator to a UserDefinedOperator method.
- Numeric literals implicitly converted to BigInteger now emit with
  the n suffix (150 → 150n) by checking ConvertedType in LiteralHandler.

Closes #54
Copilot AI review requested due to automatic review settings April 14, 2026 01:50
The top-level statement import fix also affects this sample — program.ts
now imports TodoStore via relative path instead of barrel alias.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes several TypeScript transpilation edge cases discovered while adding the new SampleOperatorOverloading sample, primarily around operator overloading, numeric casts, import generation/merging, and top-level statement handling.

Changes:

  • Add operator auto-naming and overload dispatch for user-defined operators, plus compound-assignment lowering (+=$add()).
  • Fix numeric literal/cast emission for BigInteger/decimal/bigint interop and rewrite Math.Round/Floor/Ceiling for decimal.
  • Consolidate duplicate imports, adjust top-level statement mutation detection, and add the SampleOperatorOverloading sample + TS tests.

Reviewed changes

Copilot reviewed 34 out of 34 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
tests/Metano.Tests/TypeMappingTranspileTests.cs Updates UUID import assertions to accommodate merged imports.
tests/Metano.Tests/OperatorTranspileTests.cs Adds coverage for operator auto-derived names and [Name] overrides.
src/Metano.Compiler.TypeScript/Transformation/StatementHandler.cs Fixes mutation detection scope for top-level statements.
src/Metano.Compiler.TypeScript/Transformation/RecordClassTransformer.cs Adds operator token→name mapping and operator overload dispatcher generation.
src/Metano.Compiler.TypeScript/Transformation/OperatorHandler.cs Lowers user-defined compound assignments to $opName calls.
src/Metano.Compiler.TypeScript/Transformation/LiteralHandler.cs Emits n-suffixed bigint literals when targeting BigInteger.
src/Metano.Compiler.TypeScript/Transformation/InvocationHandler.cs Rewrites System.Math calls for decimal arguments to decimal.js instance methods.
src/Metano.Compiler.TypeScript/Transformation/ImportCollector.cs Merges multiple imports from the same module path into one statement.
src/Metano.Compiler.TypeScript/Transformation/ExpressionTransformer.cs Implements explicit cast lowering for decimal/BigInteger conversions.
src/Metano.Compiler.TypeScript/PackageJsonWriter.cs Adjusts # import alias generation based on presence of root barrel.
spec/08-feature-support-matrix.md Updates roadmap/status notes for packaging and cross-package behavior.
spec/04-functional-requirements.md Extends FR-030 and adds FR-046/FR-047 requirements.
samples/SampleOperatorOverloading/SampleOperatorOverloading.csproj Adds new sample project wired to transpiler output.
samples/SampleOperatorOverloading/Program.cs Adds top-level-statement sample exercising imports and compound assignment.
samples/SampleOperatorOverloading/NoSameMoneyCurrencyException.cs Adds sample exception type for currency mismatch.
samples/SampleOperatorOverloading/Money.cs Adds Money type with overloaded operators and decimal/BigInteger conversions.
samples/SampleOperatorOverloading/Currency.cs Adds Currency enum for sample.
js/sample-todo/src/todo-list.ts Consolidates metano-runtime imports into a single line.
js/sample-todo-service/src/todos.ts Consolidates metano-runtime imports into a single line.
js/sample-todo-service/package.json Adds "#" import mapping to root barrel.
js/sample-operator-overloading/tsconfig.json Adds TS build config for the new operator-overloading JS sample.
js/sample-operator-overloading/test/money.test.ts Adds Bun tests covering generated operator helpers and dispatch.
js/sample-operator-overloading/src/program.ts Adds generated program output verifying top-level statement fixes.
js/sample-operator-overloading/src/no-same-money-currency-exception.ts Adds generated exception class output.
js/sample-operator-overloading/src/money.ts Adds generated Money class with operator helpers and dispatchers.
js/sample-operator-overloading/src/index.ts Adds generated barrel file for the sample.
js/sample-operator-overloading/src/currency.ts Adds generated Currency enum output.
js/sample-operator-overloading/package.json Adds JS package manifest for the new sample.
js/sample-issue-tracker/src/shared-kernel/user-id.ts Consolidates metano-runtime imports into a single line.
js/sample-issue-tracker/src/planning/domain/sprint.ts Consolidates metano-runtime imports into a single line.
js/sample-issue-tracker/src/issues/domain/issue-id.ts Consolidates metano-runtime imports into a single line.
js/sample-issue-tracker/src/issues/application/issue-queries.ts Consolidates metano-runtime imports into a single line (incl. type import).
Metano.slnx Adds SampleOperatorOverloading project to the solution.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/Metano.Tests/TypeMappingTranspileTests.cs Outdated
Comment thread js/sample-operator-overloading/package.json Outdated
Comment thread src/Metano.Compiler.TypeScript/Transformation/OperatorHandler.cs Outdated
Comment thread src/Metano.Compiler.TypeScript/Transformation/InvocationHandler.cs
Comment thread src/Metano.Compiler.TypeScript/Transformation/InvocationHandler.cs
Comment thread src/Metano.Compiler.TypeScript/PackageJsonWriter.cs
Comment thread tests/Metano.Tests/TypeMappingTranspileTests.cs Outdated
Comment thread tests/Metano.Tests/TypeMappingTranspileTests.cs Outdated
- Fix generate script in sample-operator-overloading package.json
- Reuse transformed LHS in compound operator assignment (avoid double eval)
- Restrict Math decimal rewrite to single-argument overloads only
- Update docstring to include Math.Abs in decimal rewrite
- Fix hasRootIndex to check outputPrefix path for subdirectory output
- Strengthen UUID import assertions with regex pattern matching
- Fix CSharpier formatting on Currency.cs
@danfma danfma merged commit 8b75089 into main Apr 14, 2026
2 checks passed
@danfma danfma deleted the fix/issues-pack branch April 14, 2026 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operator compound assignment (+=) not lowered to $add call at use site Top-level statement imports use barrel alias instead of relative paths

2 participants